/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package aviso.util;

import aviso.entity.util.FacesUtils;
import javax.faces.application.ViewHandler;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.faces.event.PhaseEvent;
import javax.faces.event.PhaseId;
import javax.faces.event.PhaseListener;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author avgusti
 */
public class URLRewritePahaseListener implements PhaseListener {

    public void afterPhase(PhaseEvent e) {
    }

    public void beforePhase(PhaseEvent e) {

        FacesContext context = FacesContext.getCurrentInstance();
        ExternalContext extContext = context.getExternalContext();
        HttpServletResponse response = (HttpServletResponse) extContext.getResponse();
        HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest();
        PhaseId phase = e.getPhaseId();
        if (e.getPhaseId() == PhaseId.RENDER_RESPONSE) {
            String contentType = "text/html; charset=UTF-8";
            extContext.getRequestMap().put("message",
                    "response is" + response);
            response.setContentType(contentType);
        }
        if (phase == PhaseId.RESTORE_VIEW) {
            try {
  
                ViewHandler handler=context.getApplication().getViewHandler();
            } catch (Exception ex) {
                //              logger.error(ex.getMessage());
                ex.printStackTrace();
            }
        }
    }

    public PhaseId getPhaseId() {
        return PhaseId.RESTORE_VIEW;
    }
}
